/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Login.java
 *
 * Created on 25/01/2009, 21:15:51
 */
package br.unisc.pdaapp.ui;

import br.unisc.pdaapp.constants.IPanels;
import br.unisc.pdaapp.meta.Student;
import br.unisc.pdaapp.meta.Tutor;
import br.unisc.pdaapp.meta.User;
import br.unisc.pdaapp.meta.ws.IMoodleMobileWS;
import com.twmacinta.util.MD5;
import br.unisc.pdaapp.webservice.client.WSHandler;

/**
 *
 * @author guilherme
 */
public class LoginPanel extends javax.swing.JPanel
{

    private static LoginPanel instance;

    /** Creates new form Login */
    private LoginPanel()
    {
        initComponents();
    }

    public static LoginPanel getInstance()
    {
        if (instance == null)
        {
            instance = new LoginPanel();
        }

        return instance;
    }

    private void doLogin(java.awt.event.ActionEvent evt)
    {
        try
        {
            String passwd = "";
            char[] pwd = pwdPasswordField.getPassword();
            for (int x = 0; x < pwd.length; x++)
            {
                passwd += pwd[x];
            }

            byte[] password = passwd.getBytes();
            MD5 md5 = new MD5(password);
            byte[] result = md5.doFinal();
            String hashResult = MD5.toHex(result);

            WSHandler wsHandler = new WSHandler();

            String userName = userNameTextField.getText();
            int resultLogin = wsHandler.doLogin(userName, hashResult);
            System.out.println("result: " + resultLogin);

            User currentUser = null;

            switch (resultLogin)
            {
                case IMoodleMobileWS.SUCCES_LOGIN_TEACHER: //Caso ser um Tutor

                    currentUser = new Tutor();
                    break;

                case IMoodleMobileWS.SUCCES_LOGIN_STUDENT: //Caso ser um Aluno

                    currentUser = new Student();
                    break;

                case IMoodleMobileWS.FAILED_LOGIN: // Incorrect login

                    messageLabel.setText("Nome ou senha incorreto!");
                    break;

                case -1: //Error on webService response.

                    messageLabel.setText("Ocorreu um erro no login!");
                    break;
            }

            if (currentUser != null) //Significa que o login ocorreu com sucesso.
            {
                currentUser.setUserName(userNameTextField.getText());

                MoodleMobileFrame.getInstance().setUser(currentUser);
                MoodleMobileFrame.getInstance().switchPanel(IPanels.COURSES_PANE);
            }

        }
        catch (Exception ex)
        {
            messageLabel.setText("Ocorreu um erro no Login");
            ex.printStackTrace();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        headerPanel = new javax.swing.JPanel();
        uniscLabel = new javax.swing.JLabel();
        moodleLabel = new javax.swing.JLabel();
        centerPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        userNameTextField = new javax.swing.JTextField();
        pwdPasswordField = new javax.swing.JPasswordField();
        jPanel2 = new javax.swing.JPanel();
        loginButton = new javax.swing.JButton();
        exitButton = new javax.swing.JButton();
        messageLabel = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setLayout(new java.awt.BorderLayout());

        headerPanel.setOpaque(false);
        headerPanel.setLayout(new java.awt.BorderLayout());

        uniscLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        uniscLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unisc/pdaapp/ui/images/logo_unisc.jpg"))); // NOI18N
        headerPanel.add(uniscLabel, java.awt.BorderLayout.NORTH);

        moodleLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        moodleLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unisc/pdaapp/ui/images/moodle2_small.jpg"))); // NOI18N
        headerPanel.add(moodleLabel, java.awt.BorderLayout.CENTER);

        add(headerPanel, java.awt.BorderLayout.NORTH);

        centerPanel.setOpaque(false);
        centerPanel.setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Login:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        centerPanel.add(jLabel1, gridBagConstraints);

        jLabel2.setText("Senha:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        centerPanel.add(jLabel2, gridBagConstraints);

        userNameTextField.setText("aluno80");
        userNameTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                userNameTextFieldKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        centerPanel.add(userNameTextField, gridBagConstraints);

        pwdPasswordField.setText("aluno80");
        pwdPasswordField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                pwdPasswordFieldKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        centerPanel.add(pwdPasswordField, gridBagConstraints);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setLayout(new java.awt.GridBagLayout());

        loginButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unisc/pdaapp/ui/images/success.png"))); // NOI18N
        loginButton.setText("Ok");
        loginButton.setContentAreaFilled(false);
        loginButton.setMaximumSize(new java.awt.Dimension(81, 33));
        loginButton.setMinimumSize(new java.awt.Dimension(81, 33));
        loginButton.setPreferredSize(new java.awt.Dimension(81, 33));
        loginButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel2.add(loginButton, gridBagConstraints);

        exitButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unisc/pdaapp/ui/images/quit.png"))); // NOI18N
        exitButton.setText("Sair");
        exitButton.setContentAreaFilled(false);
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel2.add(exitButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        centerPanel.add(jPanel2, gridBagConstraints);

        messageLabel.setFont(new java.awt.Font("Tahoma", 0, 10));
        messageLabel.setForeground(new java.awt.Color(255, 0, 0));
        messageLabel.setText(" ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(3, 5, 3, 5);
        centerPanel.add(messageLabel, gridBagConstraints);

        add(centerPanel, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void loginButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_loginButtonActionPerformed
    {//GEN-HEADEREND:event_loginButtonActionPerformed
        doLogin(evt);
}//GEN-LAST:event_loginButtonActionPerformed

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_exitButtonActionPerformed
    {//GEN-HEADEREND:event_exitButtonActionPerformed
        MoodleMobileFrame.getInstance().quit(this);
    }//GEN-LAST:event_exitButtonActionPerformed

    private void userNameTextFieldKeyPressed(java.awt.event.KeyEvent evt)//GEN-FIRST:event_userNameTextFieldKeyPressed
    {//GEN-HEADEREND:event_userNameTextFieldKeyPressed
        messageLabel.setText("");
    }//GEN-LAST:event_userNameTextFieldKeyPressed

    private void pwdPasswordFieldKeyPressed(java.awt.event.KeyEvent evt)//GEN-FIRST:event_pwdPasswordFieldKeyPressed
    {//GEN-HEADEREND:event_pwdPasswordFieldKeyPressed
        messageLabel.setText("");
    }//GEN-LAST:event_pwdPasswordFieldKeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel centerPanel;
    private javax.swing.JButton exitButton;
    private javax.swing.JPanel headerPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton loginButton;
    private javax.swing.JLabel messageLabel;
    private javax.swing.JLabel moodleLabel;
    private javax.swing.JPasswordField pwdPasswordField;
    private javax.swing.JLabel uniscLabel;
    private javax.swing.JTextField userNameTextField;
    // End of variables declaration//GEN-END:variables
}
